package com.administration.controller;

import com.administration.constants.Result;
import com.administration.dao.SysRoleMapper;
import com.administration.pojo.SysRole;
import com.administration.utils.ResultData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sysRole")
public class SysRoleController {

    @Resource
    private SysRoleMapper sysRoleMapper;

    //角色信息展示主界面
    @RequestMapping("/role.do")
    public String roleIndex(){
        return "/static/role/layui_role_index.html";
    }

    //查询所有角色信息/查询单个
    @ResponseBody
    @RequestMapping("/selectAllRole.do")
    public ResultData selectAllRole(SysRole sysRole,String startTime,String endTime){
        Map<String,Object> map = new HashMap<>();
        map.put("roleId",sysRole.getRoleId());
        map.put("roleKey",sysRole.getRoleKey());
        map.put("status",sysRole.getStatus());

        //用于日期判断
        StringBuffer strTime = new StringBuffer();

        List<SysRole> list = sysRoleMapper.selectAllRole(map);
        List<SysRole> lists = new ArrayList<>();
        for (SysRole sr:list){
            if ("0".equals(sr.getStatus())){
                sr.setStatus("正常");
            }else {
                sr.setStatus("停用");
            }

            //日期判断添加结束时间结束值
            if(strTime.length() <= 0){
                strTime.append(endTime);
                strTime.append(" 59:59:59");
                endTime = strTime.toString();
            }

            //日期搜索判断
            //对查询日期进行判断，值为空不进行判断
            if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
                String createTime = sr.getCreateTime();
                if (createTime.compareTo(startTime) >= 0 && createTime.compareTo(endTime) <= 0){
                    lists.add(sr);
                }
            }else {
                lists.add(sr);
            }
        }

        //List<SysRole> list = sysRoleMapper.selectRoleByUserName(sysUser.getLoginName());
        return new ResultData<>(Result.SUCCESS.getCode(), "",lists.size(), lists);
    }

    //新增角色信息
    @ResponseBody
    @RequestMapping("/addRole.do")
    public int addRole(SysRole sysRole){
        int addRes = sysRoleMapper.addRole(sysRole);
        return addRes;
    }

    //修改角色信息
    @ResponseBody
    @RequestMapping("/updateRole.do")
    public int updateRole(SysRole sysRole){
        int updateRes = sysRoleMapper.updateRole(sysRole);
        return updateRes;
    }

    //删除角色信息
    @ResponseBody
    @RequestMapping("/deleteRole.do")
    public int deleteRole(Integer roleId){
        int deleteRes = sysRoleMapper.deleteRole(roleId);
        return deleteRes;
    }
}
